package com.bytz.modules.cms.reseller.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.common.system.base.controller.BytzController;
import com.bytz.common.system.query.QueryGenerator;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.common.util.ExcelUtil;
import com.bytz.modules.cms.reseller.constant.ResellerConstant;
import com.bytz.modules.cms.reseller.entity.Reseller;
import com.bytz.modules.cms.reseller.model.ResellerModel;
import com.bytz.modules.cms.reseller.model.ResellerProductModel;
import com.bytz.modules.cms.reseller.model.response.ResellerListVo;
import com.bytz.modules.cms.reseller.service.IAdminResellerService;
import com.bytz.modules.cms.reseller.service.IResellerProductService;
import com.bytz.modules.cms.util.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "经销商")
@RequestMapping("/reseller")
@PreAuthorize("hasAuthority('" + AdminAuthConstants.Reseller.EDIT + "')")
public class ResellerController extends BytzController<Reseller, IAdminResellerService> {

    @Autowired
    private IAdminResellerService adminResellerService;

    @Autowired
    private IResellerProductService resellerProductService;

    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public Result<ResellerModel> queryById(@RequestParam(value = "id") String id) {

        return Result.ok(adminResellerService.getModelById(id));
    }

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<Page<ResellerListVo>> page(Reseller reseller,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                       HttpServletRequest req) {

        Page<Reseller> page = adminResellerService.page(
                new Page<>(pageNo, pageSize),
                QueryGenerator.initLambdaQueryWrapper(reseller, req.getParameterMap())
        );
        Page<ResellerListVo> resellerListVoPage = PageUtils.convertToVoPage(page, ResellerListVo.class);
        return Result.ok(resellerListVoPage);
    }

    @ApiOperation(value = "获取全部经销商", notes = "获取全部经销商")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    public Result<List<Reseller>> listAll() {

        return Result.ok(adminResellerService.list());
    }

    @ApiOperation(value = "启用", notes = "启用")
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public Result<Boolean> enable(@RequestParam String id) {

        return Result.ok(adminResellerService.changeStatus(id, ResellerConstant.ResellerStatus.ENABLE));
    }

    @ApiOperation(value = "禁用", notes = "禁用")
    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    public Result<Boolean> disable(@RequestParam String id) {

        return Result.ok(adminResellerService.changeStatus(id, ResellerConstant.ResellerStatus.DISABLE));
    }

    @ApiOperation(value = "重置密码", notes = "重置密码")
    @RequestMapping(value = "/resetPass", method = RequestMethod.POST)
    public Result<Boolean> resetPass(@RequestParam String id) {

        return Result.ok(adminResellerService.resetPassword(id));
    }

    @ApiOperation(value = "删除一条", notes = "删除一条")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result<Boolean> delete(@RequestParam String id) {

        return Result.ok(adminResellerService.checkAndRemoveById(id));
    }

    @ApiOperation(value = "获取导入模板", notes = "获取导入模板")
    @RequestMapping(value = "/template", method = RequestMethod.POST)
    public void template(HttpServletResponse response) throws Exception {

        ExcelUtil.exportTemp("导入经销商", "Sheet1", Reseller.class, response);
    }

    @ApiOperation(value = "导入用户", notes = "导入用户")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request) {

        List<Reseller> ecProductList = ExcelUtil.importGroup(request, Reseller.class, null);
        adminResellerService.importExcel(ecProductList);
        return Result.ok();
    }

    @ApiOperation(value = "生成价格表", notes = "生成价格表")
    @RequestMapping(value = "/priceList", method = RequestMethod.POST)
    public void exportXls(HttpServletRequest req, HttpServletResponse response, Reseller reseller) {

        adminResellerService.generatePriceList(
                response,
                QueryGenerator.initLambdaQueryWrapper(reseller, req.getParameterMap())
        );
    }

    @ApiOperation(value = "保存或编辑", notes = "保存或编辑")
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    public Result<ResellerModel> saveOrUpdate(@RequestBody ResellerModel model) {

        return Result.ok(adminResellerService.saveOrUpdateSaveObject(model, true));
    }

    @ApiOperation(value = "通过name查询", notes = "通过name查询")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public Result<List<Reseller>> searchByName(@RequestParam(value = "name") String name) {

        return Result.ok(adminResellerService.searchByName(name));
    }

    @ApiOperation(value = "独占商品优惠列表", notes = "独占商品优惠列表")
    @RequestMapping(value = "/discount", method = RequestMethod.GET)
    public Result<IPage<ResellerProductModel>> discountList(@RequestParam String id,
                                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return Result.ok(resellerProductService.getModelByResellerId(id, new Page<>(pageNo, pageSize)));
    }
}

